package com.z.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.z.common.pro.vo.TaskVo;
import com.z.common.web.base.BaseController;
import com.z.common.web.vo.request.PageVo;
import com.z.common.web.vo.response.Result;
import com.z.sys.entity.Notice;
import com.z.sys.entity.SysMenu;
import com.z.sys.entity.User;
import com.z.sys.service.INoticeService;
import com.z.sys.service.ISysMenuService;
import com.z.sys.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.task.Task;
import org.camunda.bpm.engine.task.TaskQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * 机构管理Controller
 *
 * @author z
 * @date 2022-11-07
 */
@RestController
@RequestMapping("/system/common")
@Api(tags = "公共页面")
public class CommonController extends BaseController {

    private String prefix = "system/common";

    @Autowired
    private IUserService userService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private ISysMenuService sysMenuService;

    @Autowired
    private INoticeService noticeService;
    /**
     * 系统首页
     * */
    @GetMapping("index")
    @ApiOperation(value = "系统首页页面")
    public ModelAndView main() {
        return jumpPage(prefix + "/index");
    }
    /**
     * 快捷菜单
     * */
    @GetMapping("proMenu")
    @ApiOperation(value = "快捷菜单接口")
    public Result proMenu() {
        List<SysMenu> list = sysMenuService.selectPageMenuForCurrentUser();
        return Result.success(list);
    }

    /**
     * 待办
     * */
    @GetMapping("/{userId}/todo")
    @ApiOperation(value = "待办流程数据接口")
    public Result todo(PageVo pageDomain, @PathVariable("userId") String userId) {
        TaskQuery query = taskService.createTaskQuery();
        List<Task> list = query.taskAssignee(userId).orderByTaskCreateTime().desc().listPage(0, 5);

        List<TaskVo> data = new ArrayList<>();

        for(Task task : list){
            TaskVo taskVo = new TaskVo();
            taskVo.setId(task.getId());
            taskVo.setName(task.getName());
            if(StringUtils.isNotEmpty(task.getProcessDefinitionId())){
                String[] strs = task.getProcessDefinitionId().split(":");
                taskVo.setDefinitionId(strs[2]);
                taskVo.setType(strs[0]);
                taskVo.setVersion(strs[1]);
            }
            taskVo.setInstanceId(task.getProcessInstanceId());
            taskVo.setCreateTime(task.getCreateTime());
            data.add(taskVo);
        }
        int count = (int) query.orderByTaskCreateTime().desc().count();

        return Result.table(data, count);
    }
    /**
     * 信息提醒
     * */
    @GetMapping("/{userId}/notice")
    @ApiOperation(value = "待办流程数据接口")
    public Result notice(PageVo pageDomain, @PathVariable("userId") String userId) {

        List<Notice> list = noticeService.list(new QueryWrapper<Notice>()
                .eq("accept", userId).or().eq("type", "public")
                .orderByDesc("is_top")
                .orderByDesc("create_time")
                .last("limit 5"));
        return Result.table(list, list.size());
    }
    /**
     * 信息提醒
     * */
    @GetMapping("/notice/{noticeId}")
    @ApiOperation(value = "待办详情页面")
    public ModelAndView noticeDetial(Model model, @PathVariable("noticeId") String noticeId) {
        Notice notice = noticeService.getOne(new QueryWrapper<Notice>().eq("id", noticeId).eq("del", "0"));
        User user = userService.getById(notice.getSender());
        if(user == null){
            notice.setSender("");
        }else{
            notice.setSender(user.getUsername());
        }
        model.addAttribute("notice", notice);
        return jumpPage(prefix + "/noticeDetail");
    }
    /**
     * 文件上传页面
     * @return
     */
    @GetMapping("/fileUploadMain")
    @ApiOperation(value = "文件上传页面")
    public ModelAndView fileUploadMain(Model model, String fileId, String fileName, boolean isPre) {
        model.addAttribute("fileId", fileId);
        model.addAttribute("fileName", fileName);
        model.addAttribute("isPre", isPre);
        return jumpPage(prefix + "/fileUploadMain");
    }
    /**
     * 用户查询页面
     * @return
     */
    @GetMapping("/userSelectMain")
    @ApiOperation(value = "人员查询页面")
    public ModelAndView userSelectMain(Model model, String userName, String userId, String m) {
        model.addAttribute("userName", userName);
        model.addAttribute("userId", userId);
        model.addAttribute("m", m);
        return jumpPage(prefix + "/userSelectMain");
    }
    /**
     * 用户多选查询页面
     * @return
     */
    @GetMapping("/userSelectMultipleMain")
    @ApiOperation(value = "人员多选查询页面")
    public ModelAndView userSelectMultipleMain(Model model, String userName, String userId, String m) {
        model.addAttribute("userName", userName);
        model.addAttribute("userId", userId);
        model.addAttribute("m", m);
        return jumpPage(prefix + "/userSelectMultipleMain");
    }

    /**
     * 角色查询页面
     * @return
     */
    @GetMapping("/roleSelectMain")
    @ApiOperation(value = "角色查询页面")
    public ModelAndView roleSelectMain(Model model, String roleName, String roleId) {
        model.addAttribute("roleName", roleName);
        model.addAttribute("roleId", roleId);
        return jumpPage(prefix + "/roleSelectMain");
    }

    /**
     * 公共导入页面
     * @return
     */
    @GetMapping("/importMain")
    @ApiOperation(value = "公共导入页面")
    public ModelAndView importMain(Model model,
                                   @ApiParam(value = "dUrl", name = "下载模板地址", required = true) String dUrl,
                                   @ApiParam(value = "uUrl", name = "导入地址地址", required = true) String uUrl) {
        model.addAttribute("dUrl", dUrl);
        model.addAttribute("uUrl", uUrl);
        return jumpPage(prefix + "/import");
    }
}
